package com.hwadee.ems.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hwadee.ems.domain.dto.Result;
import com.hwadee.ems.domain.dto.UserDTO;
import com.hwadee.ems.domain.po.User;
import com.hwadee.ems.domain.vo.UserVO;
import com.hwadee.ems.enums.UserRole;
import com.hwadee.ems.service.IUserService;
import com.hwadee.ems.domain.dto.LoginFormDTO;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author EnosElinsa
 * @since 2024/06/21
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    /**
     * 用户登录
     * @param loginForm 登录表单
     * @return 用户信息
     */
    @PostMapping("/login")
    public Result<UserDTO> login(@RequestBody LoginFormDTO loginForm) {
        return userService.login(loginForm);
    }

    /**
     * 登出功能
     * @return 无
     */
    @PostMapping("/logout")
    public Result<Void> logout(HttpServletRequest request) {
        return userService.logout(request);
    }

    @GetMapping("/list_teachers")
    public Result<List<UserVO>> listTeachers() {
        return Result.ok(
            userService.lambdaQuery()
                .eq(User::getRole, UserRole.TEACHER)
                .list()
                .stream()
                .map(UserVO::new)
                .collect(Collectors.toList())
        );
    }

}
